package views.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalendarPanel extends JPanel {

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JPanel daysContainer;
	private JPanel daysOfTheWeek;
	private JPanel intestazione;
	private JLabel monthAndYear;
	private List<JButton> dayButtons = new ArrayList<JButton>(31);
	private Font monthFont = new Font("Segoe UI", Font.PLAIN, 30);
	private Font daysFont = new Font("Segoe UI", Font.PLAIN, 25);
	private Font fontDaysOfTheWeek = new Font("Segoe UI", Font.PLAIN, 22);

	public CalendarPanel() {

		super();
		this.setLayout(new BorderLayout());
		daysOfTheWeek = new JPanel();
		intestazione = new JPanel();
		monthAndYear = new JLabel();
		monthAndYear.setOpaque(true);
		daysContainer = new JPanel();
		daysContainer.setLayout(new GridLayout(6, 7, 1, 1));
		daysOfTheWeek.setLayout(new GridLayout(1, 7, 1, 1));
		monthAndYear.setFont(monthFont);
		monthAndYear.setBackground(GameColors.BLU);
		monthAndYear.setHorizontalAlignment(JTextField.CENTER);
		monthAndYear.setForeground(Color.WHITE);
		monthAndYear.setText(getMonthAndYear());
		intestazione.setLayout(new GridLayout(2, 1));
		add(daysContainer, BorderLayout.CENTER);
		intestazione.add(monthAndYear);
		intestazione.add(daysOfTheWeek);
		add(intestazione, BorderLayout.NORTH);

		for (int i = 0; i < 7; i++) {
			JLabel currentDayName = new JLabel();
			currentDayName.setForeground(GameColors.BLU);
			currentDayName.setFont(fontDaysOfTheWeek);
			currentDayName.setHorizontalAlignment(JTextField.CENTER);
			switch (i) {
			case 0:
				currentDayName.setText("Lunedì");
				break;
			case 1:
				currentDayName.setText("Martedì");
				break;
			case 2:
				currentDayName.setText("Mercoledì");
				break;
			case 3:
				currentDayName.setText("Giovedì");
				break;
			case 4:
				currentDayName.setText("Venerdì");
				break;
			case 5:
				currentDayName.setText("Sabato");
				break;
			case 6:
				currentDayName.setText("Domenica");
				break;
			}
			daysOfTheWeek.add(currentDayName);
		}

		GregorianCalendar gc = new GregorianCalendar();
		int currentMonth = gc.get(GregorianCalendar.MONTH);
		int currentYear = gc.get(GregorianCalendar.YEAR);
		int totalDays = gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
		int firstDayNameInThisMonth = (getFirstDay() + 6) % 7;
		// se domenica firstDay vale 0; correggo a 7
		if (firstDayNameInThisMonth == 0)
			firstDayNameInThisMonth = 7;
		int currentDay = 1;
		int today = gc.get(GregorianCalendar.DAY_OF_MONTH);

		// PRIMA RIGA DEL CALENDARIO
		for (int i = 1; i <= 7; i++) {
			JButton dayButton = newDayButton();
			if (i >= firstDayNameInThisMonth) {
				dayButton.setText("" + currentDay);
				dayButton.setEnabled(currentDay <= today);
				if (currentDay <= today) {
					dayButton.setName(currentYear + "/" + (currentMonth + 1)
							+ "/" + currentDay);
					dayButtons.add(dayButton);
				}
				currentDay++;
				// /////// if ( currentDay < today)
				// /////// dayLabel.setEnabled(false);
			} else
				dayButton.setEnabled(false);
			daysContainer.add(dayButton);
		}
		// SUCCESSIVE RIGHE DEL CALENDARIO
		for (int i = 0; i < 35; i++) {
			JButton dayButton = newDayButton();
			if (currentDay <= totalDays) {
				dayButton.setText("" + currentDay);
				dayButton.setEnabled(currentDay <= today);
				if (currentDay <= today) {
					dayButton.setName(currentYear + "/" + (currentMonth + 1)
							+ "/" + currentDay);
					dayButtons.add(dayButton);
				}
				currentDay++;
				// /////// if ( currentDay < today)
				// /////// dayLabel.setEnabled(false);
			} else
				dayButton.setEnabled(false);
			daysContainer.add(dayButton);

		}

		dayButtons.get(today - 1).setBackground(GameColors.BLU);
		dayButtons.get(today - 1).setForeground(Color.WHITE);
	}

	public static int getFirstDay() {
		GregorianCalendar c = new GregorianCalendar();
		c.set(GregorianCalendar.DAY_OF_MONTH, 1);
		return c.get(GregorianCalendar.DAY_OF_WEEK);
	}

	public static String getMonthAndYear() {
		GregorianCalendar gc = new GregorianCalendar();
		int y = gc.get(GregorianCalendar.YEAR);
		int m = gc.get(GregorianCalendar.MONTH);
		switch (m) {
		case GregorianCalendar.JANUARY:
			return "Gennaio " + y;
		case GregorianCalendar.FEBRUARY:
			return "Febbraio " + y;
		case GregorianCalendar.MARCH:
			return "Marzo " + y;
		case GregorianCalendar.APRIL:
			return "Aprile " + y;
		case GregorianCalendar.MAY:
			return "Maggio " + y;
		case GregorianCalendar.JUNE:
			return "Giugno " + y;
		case GregorianCalendar.JULY:
			return "Luglio " + y;
		case GregorianCalendar.AUGUST:
			return "Agosto " + y;
		case GregorianCalendar.SEPTEMBER:
			return "Settembre " + y;
		case GregorianCalendar.OCTOBER:
			return "Ottobre " + y;
		case GregorianCalendar.NOVEMBER:
			return "Novembre " + y;
		case GregorianCalendar.DECEMBER:
			return "Dicembre " + y;
		default:
			throw new RuntimeException();
		}
	}

	private JButton newDayButton() {
		JButton day = new JButton();
		day.setBorder(BorderFactory.createEmptyBorder());
		day.setForeground(GameColors.GRIGIO_SCURO);
		day.setBackground(Color.WHITE);
		day.setFont(daysFont);
		day.setHorizontalAlignment(JTextField.CENTER);
		return day;
	}

	public List<JButton> getDayButtons() {
		return dayButtons;
	}
}
